package com.office.onlineInformation.draftBox.controller;


import com.office.onlineInformation.draftBox.pojo.OaOnlineMessage;
import com.office.onlineInformation.draftBox.service.MessageSearchService;
import com.office.onlineInformation.draftBox.service.draftBoxService;
import com.office.personalOffice.personalInfo.model.LoginUser;
import com.office.personalOffice.personalInfo.model.TbStaff;
import com.office.util.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 88168
 * @since 2022-10-25
 */
@RestController
@RequestMapping("/draftBox")
public class draftBoxController {
    @Autowired
    public draftBoxService service;
    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    public CacheManager cacheManager;

    @Autowired
    public MessageSearchService messageSearchService;

    /**
     * 添加草稿箱留言
     * @param onlineMessage
     * @return
     *
     * CachePut 把方法的返回值放入缓存中
     * value 缓存的名称
     * key 缓存的key
     */
    @CachePut(value = "draftCache",key = "#onlineMessage.recevierNo")
    @RequestMapping("/add")
    public String addMessage(@RequestBody OaOnlineMessage onlineMessage, Authentication authentication){
        TbStaff user = getUser(authentication);
        onlineMessage.setCreatedId(user.getId());
        String message = service.addMessage(onlineMessage);
        return message;
    }
    /**
     * 删除草稿箱留言
     * @param informationId
     * @return
     */
    @CacheEvict(value = "draftCache",allEntries = true)
    @RequestMapping("/delete/{informationId}")
    public String deleteMessage(@PathVariable int informationId){
        // 获取以"email"开头的键
        Set<String> keys = redisTemplate.keys("draftBox:" + "*");

        // 遍历并删除每个键
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        String message = service.deleteMessage(informationId);
        return message;
    }
    /**
     * 修改留言
     * @param onlineMessage
     * @return
     */
    @CacheEvict(value = "draftCache",allEntries = true)
    @RequestMapping("/update")
    public String updateMessage(@RequestBody OaOnlineMessage onlineMessage){
        String message = service.updateMessage(onlineMessage);
        return message;
    }

    /**
     * 查询草稿箱留言
     * @return
     */
    @RequestMapping("/get")
    @Cacheable(value = "draftCache",key="'draft:'+#customManage.title + '-' +#customManage.receiver+ '-' +#customManage.sender ",unless = "#result == null")
    public PageModel<OaOnlineMessage> getList(OaOnlineMessage customManage, PageModel<OaOnlineMessage> pageModel,Authentication authentication) throws IOException {
//        String key = "draftBox:"+pageModel.getPageNum()+":"+customManage.getTitle()+":"+customManage.getReceiver()+":"+customManage.getSender();
//        PageModel<OaOnlineMessage>  res =( PageModel<OaOnlineMessage>) redisTemplate.opsForValue().get(key);
//        if (res!=null){
//            return res;
//        }
        TbStaff user = getUser(authentication);
        customManage.setCreatedId(user.getId());
        PageModel<OaOnlineMessage> list = service.getList(customManage, pageModel);
//        redisTemplate.opsForValue().set(key,list);
        return list;
    }
    /**
     * 根据id查询留言
     * @param Id
     * @return
     */
    @RequestMapping("/getOne/{Id}")
    @Cacheable(value = "draftCache",key="#id",condition = "#result != null")
    public OaOnlineMessage getById(@PathVariable int Id){
        OaOnlineMessage message = service.getById(Id);
        messageSearchService.insert(message.getTitle(),message.getCreatedId());
        return message;
    }
    public static TbStaff getUser(Authentication authentication){
        LoginUser principal =(LoginUser) authentication.getPrincipal();
        TbStaff user = principal.getUser();
        return user;
    }
}

